package com.detech.sboh.base.log.serviceimpl;

import com.detech.sboh.base.hibernate.log.dao.TbLogP6spyDAO;
import com.detech.sboh.base.hibernate.log.dao.TbLogSysDAO;
import com.detech.sboh.base.hibernate.log.entity.TbLogP6Spy;
import com.detech.sboh.base.hibernate.log.entity.TbLogSys;
import com.detech.sboh.base.log.service.LogService;
import com.detech.sboh.base.page.Page;
import com.detech.sboh.base.page.PageDAO;
import com.detech.sboh.base.util.UserFun;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Transactional(rollbackFor = Exception.class)
public class LogServiceImpl implements LogService {

    @Value(value = "${spring.application.name}")
    private String xmmc;
    @Resource
    private PageDAO pageDAO;
    @Resource
    private TbLogSysDAO tbLogSysDAO;
    @Resource
    private TbLogP6spyDAO tbLogP6spyDAO;

    @Override
    public void saveSysLog(TbLogSys sysLogEntity){
        tbLogSysDAO.save(sysLogEntity);
    }

    @Override
    public void writeDB(String message) {
        String[] msgSz = null;
        /** 检查信息合法性 **/
        boolean flag = false;
        if(message!=null && message.trim().length()>0){
            msgSz = message.split("&");
            //信息拆分后，长度为3
            if(msgSz.length==3){
                flag = true;
            }
        }
        //性能监控信息格式不符
        if(flag==false){
            return;
        }
        int xhsj = new Integer(msgSz[0]);//消耗时间
        String ybySql = msgSz[1];//预编译SQL
        String paramSql = msgSz[2];//带参数SQL
        TbLogP6Spy entity = new TbLogP6Spy();
        entity.setId(UserFun.getUUID32());
        entity.setLogTime(new Date());
        entity.setXmmc(xmmc);
        entity.setParamSql(paramSql);
        entity.setYbySql(ybySql);
        entity.setXhsj(xhsj);
        tbLogP6spyDAO.save(entity);
    }

    @Override
    public Page loadSysLogList(Page page, String content, String logType, String module, String operator, String ip, String logTime)  {
        String hql = "from TbLogSys t where 1=1 ";
        if(logType!=null && logType.trim().length()>0){
            hql = hql + " and t.logType=? ";
            page.addParam(logType);
        }
        if(module!=null && module.trim().length()>0){
            hql = hql + " and t.module=? ";
            page.addParam(module);
        }
        if(ip!=null && ip.trim().length()>0){
            hql = hql + " and t.ip=?";
            page.addParam(ip);
        }
        if(content!=null && content.trim().length()>0){
            hql = hql + " and t.content like ? ";
            page.addParam("%"+content+"%");
        }
        if(operator!=null && operator.trim().length()>0){
            hql = hql + " and t.operator like ? ";
            page.addParam("%"+operator+"%");
        }
        if(logTime!=null && logTime.trim().length()>0){
            hql = hql + " and (t.logTime > to_date(?,'yyyy-mm-dd') and t.logTime < to_date(?,'yyyy-mm-dd') + 1) ";
            page.addParam(logTime);
            page.addParam(logTime);
        }
        hql = hql + " order by t.logTime desc ";
        page.setHql(hql);
        return pageDAO.queryDataList(page);
    }

    @Override
    public Page loadP6spyLogList(Page page, String ybySql,String paramSql,Integer xhsj,String xmmc, String logTime)  {
        String hql = "from TbLogP6Spy t where 1=1 ";
        if(xmmc!=null && xmmc.trim().length()>0){
            hql = hql + " and t.xmmc=? ";
            page.addParam(xmmc);
        }
        if(xhsj!=null && xhsj.toString().trim().length()>0){
            hql = hql + " and t.xhsj >=? ";
            page.addParam(xhsj);
        }
        if(ybySql!=null && ybySql.trim().length()>0){
            hql = hql + " and t.ybySql like ? ";
            page.addParam("%"+ybySql+"%");
        }
        if(paramSql!=null && paramSql.trim().length()>0){
            hql = hql + " and t.paramSql like ? ";
            page.addParam("%"+paramSql+"%");
        }
        if(logTime!=null && logTime.trim().length()>0){
            hql = hql + " and (t.logTime > to_date(?,'yyyy-mm-dd') and t.logTime < to_date(?,'yyyy-mm-dd') + 1) ";
            page.addParam(logTime);
            page.addParam(logTime);
        }
        hql = hql + " order by t.logTime desc ";
        page.setHql(hql);
        return pageDAO.queryDataList(page);
    }

    @Override
    public TbLogSys loadSysLogDetail(String id)  {
        return tbLogSysDAO.findById(id);
    }

    @Override
    public TbLogP6Spy loadP6spyLogDetail(String id)  {
        return tbLogP6spyDAO.findById(id);
    }

}
